﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TypeMock.ArrangeActAssert;
using Microsoft.SharePoint;

namespace BPC.Magic8Ball.Test
{
    [TestFixture]
    public class SharePoint
    {
        Magic8Ball.SharePoint _sharePoint;

        [SetUp]
        public void SetUp()
        {
            _sharePoint = new BPC.Magic8Ball.SharePoint();
            FakeList();
        }

        [Test]
        public void ShouldReturnListOfAnswersFromSharePointList()
        {
            //Arrange

            //Act
            List<String> answers = _sharePoint.GetAnswersFromList("ListName");

            //Assert
            Assert.IsNotEmpty(answers);
        }

        [Test]
        public void ShouldReturnSpecifcAnswerFromSharePointList()
        { 
            //Arrange

            //Act
            List<String> answers = _sharePoint.GetAnswersFromList("ListName");           
        
            //Assert
            Assert.AreEqual("AnswerOne", answers[1]);
        }


        //Fake the SharePoint List values and Context
        private void FakeList()
        {
            SPList fakeList = Isolate.Fake.Instance<SPList>();
            Isolate.WhenCalled(() => SPContext.Current.Web.Lists["ListName"]).WillReturn(fakeList);

            //Now fake the list return values
            SPListItem fakeAnswerOne = Isolate.Fake.Instance<SPListItem>();
            SPListItem fakeAnswerTwo = Isolate.Fake.Instance<SPListItem>();


            Isolate.WhenCalled(() => fakeAnswerOne.Title).WillReturn("AnswerZero");
            Isolate.WhenCalled(() => fakeAnswerTwo.Title).WillReturn("AnswerOne");

            Isolate.WhenCalled(() => fakeList.Items).WillReturnCollectionValuesOf(new List<SPItem>
                                  {
                                      fakeAnswerOne,
                                      fakeAnswerTwo
                                  });
        }
    }
}
